<?php
/**
 * 查看权限列表
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Report_Report");
apf_require_class("Report_Urls");
apf_require_class("Bll_Hr_Property");
class Report_SettingController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $userdata =  $this->user_info;
        if(!$this->check_role_auth()){
            $this->show_tips('ERROR_AUTH');
        }
        $bu_list = Bll_Hr_Property::get_business_list();
        $city_list = Bll_Hr_Property::get_sort_city_list();
        $report_bll = new Bll_Report_Report();
        if($this->request->is_post_method()){
            $dep_ids = $params['department_ids'];
            if(empty($dep_ids)){
                $this->show_tips('ERROR_PARAM');
            }
            if(empty($params['user_id'])){
                $this->show_tips('ERROR_PARAM');
            }
            $insert=array();
            foreach($dep_ids as $val){
                list($fid,$buid,$dep_id,$city_id,$dep_name) = explode('-',$val);
                $insert[$dep_id] = array(
                        'user_id'=>$params['user_id'],
                        'function_id'=>$fid,
                        'bu_id'=>$buid,
                        'department_id'=>$dep_id,
                        'department_name'=>$city_list[$city_id].$bu_list[$buid]['bu_name'].$dep_name
                        );
            }

            $report_bll->multi_insert($insert);
            list($name,$job)=explode('-',$params['userName']);
            $this->response->redirect(Report_Urls::setting_url().'?user_id='.$params['user_id'].'&name='.base64_encode($name));
        }
        $hroa_bll = new Bll_Hr_OutInterface();
        $res = $hroa_bll->get_department_by_business();
        foreach($res as $val){
            $bu_info[$val['bu_id']][$val['city_id']][]=array('department_id'=>$val['function_id'].'-'.$val['bu_id'].'-'.$val['department_id'].'-'.$val['city_id'].'-'.$val['department_name'],'department_name'=>$val['department_name']);
        }
        if(!empty($params['name'])){
            $params['name'] = base64_decode($params['name']);
        }
        if(!empty($params['user_id'])){
            $list = $report_bll->get_department_by_uid($params['user_id']);
            if(!empty($list)){
                foreach($list as $val){
                    $data[$val['bu_id']][$val['id']] = $val['department_name'];//事业部下的部门
                }
            }
        }

        $this->request->set_attribute('params', $params);
        $this->request->set_attribute('data', $data);
        $this->request->set_attribute('bu_info', $bu_info);
        $this->request->set_attribute('bu_list', $bu_list);
        $this->request->set_attribute('city_list', $city_list);
        return "Report_Setting";
    }
}